package javaweb_work01.main.Servlet;

import javaweb_work01.main.Dao.SetStudentsDao;
import javaweb_work01.main.Dao.SetStudentsDaoImp;
import javaweb_work01.main.Entity.Students;
import javaweb_work01.main.Service.*;
import javaweb_work01.main.Utils.PageContrl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "SetServlet", urlPatterns = "/set")
public class SetServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决POST接收乱码
        request.setCharacterEncoding("utf-8");
        //得到修改页面中输入的数据
        int setNum = Integer.parseInt(request.getParameter("Num"));
        String setName = request.getParameter("Name");
        String setSex = request.getParameter("Sex");
        String setDate = request.getParameter("Date");
        String setEmail = request.getParameter("Email");
        String setRemark = request.getParameter("Remark");
        int setClassId = Integer.parseInt(request.getParameter("classId"));
        //将得到的数据构造新的Students对象，作为参数传入Service，最后调用Dao层的方法，修改对应学号的学员信息
        SetService setService = new SetService();
        setService.setStudentsService(new Students(setNum,setName,setSex,setDate,setEmail,setRemark,setClassId));

        //获取主页需要显示的信息，构建主页
        //声明CountService类的对象，以便调用方法
        CountService countService = new CountService();
        //调用Service中的方法，最后调用Dao层方法，返回数据库中的数据总数，即学员总数
        int count = countService.countStudentsService();
        //利用得到的学员总数构造新的PageContrl对象，用于分页显示，此处为从第0条开始，显示10条
        PageContrl pageContrl = new PageContrl(0,10,count);
        //声明ShowService类的对象
        ShowService showService = new ShowService();
        //利用ShowService类的对象调用方法，最后调用Dao层方法，返回数据库中该页需要显示的学员信息
        List<Students> students = showService.showStudentsService(pageContrl);

        //获取当前所有班级编号
        //调用Service层方法，进入Dao层并得到班级编号结果集
        CheckClassesIdservice checkClassesIdservice = new CheckClassesIdservice();
        List<Object[]> objects = checkClassesIdservice.checkClassesIdService();
        //准备一个集合用于存放结果集中的班级编号
        ArrayList<Integer> allClassesId = new ArrayList<>();
        for(Object[] j:objects){
            allClassesId.add((Integer) j[0]);
        }

        //将得到的PageContrl对象，Students对象，班级编号集合放入session中实现共享
        request.getSession().setAttribute("pageContrl",pageContrl);
        request.getSession().setAttribute("showStudents",students);
        request.getSession().setAttribute("allClassesId",allClassesId);
        //跳转到主页
        response.sendRedirect("main.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
